package cn.david.org.controller;


import cn.david.basic.util.AjaxResult;
import cn.david.basic.util.PageList;
import cn.david.org.domain.Employee;
import cn.david.org.query.EmployeeQuery;
import cn.david.org.service.IEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/emp")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Employee employee){
        try {
            if (employee.getId()==null){
            employeeService.add(employee);
            }else {
                employeeService.upadte(employee);
            }
            return AjaxResult.me();

        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("添加失败"+e.getMessage());
        }

    }
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id ){
        try {
            employeeService.del(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("删除失败"+e.getMessage());
        }


    }
    @GetMapping("/{id}")
    public Employee queryById(@PathVariable("id") Long id){
        return employeeService.getById(id);

    }
    @GetMapping
    public List<Employee> getAll(){
        return employeeService.getAll();
    }
    @PostMapping
    public PageList<Employee> list(@RequestBody EmployeeQuery query){

        return employeeService.queryData(query);
    }
    @PatchMapping
    public AjaxResult deleteBys(@RequestBody Long[] ids){
        try {
            employeeService.deleteByIds(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("删除失败"+e.getMessage());
        }

    }


}
